package com.xq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xq.dto.UmsOrganizations.*;
import com.xq.entity.UmsOrganizations;
import com.xq.exception.BusinessException;
import com.xq.mapper.UmsOrganizationsMapper;
import com.xq.service.IUmsOrganizationsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.service.IUmsUserOrganizationService;
import com.xq.vo.DataBaseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuven
 * @since 2025-05-26
 */
@Service
public class UmsOrganizationsServiceImpl extends ServiceImpl<UmsOrganizationsMapper, UmsOrganizations> implements IUmsOrganizationsService {

    @Autowired
    private UmsOrganizationsMapper umsOrganizationsMapper;

    @Autowired
    private IUmsUserOrganizationService umsUserOrganizationService;

    @Override
    public void addOrganizations(UmsOrganizationsCreatedDTO createdDTO) {
        UmsOrganizations umsOrganizations = new UmsOrganizations();
        umsOrganizations.setOrgName(createdDTO.getOrgName());
        umsOrganizations.setParentOrgId(createdDTO.getParentOrgId());
        umsOrganizations.setOrgType(createdDTO.getOrgType());
        int insert = umsOrganizationsMapper.insert(umsOrganizations);
        if (insert < 0) {
            throw new BusinessException(DataBaseEnum.INSERTION_FAILED);
        }
    }

    @Override
    public void deleteOrganizations(String organizationId) {
        int i = umsOrganizationsMapper.deleteById(organizationId);
        if (i < 0) {
            throw new BusinessException(DataBaseEnum.DELETE_FAILED);
        }
    }

    @Override
    public void updateOrganizations(UmsOrganizationsUpdateDTO updateDTO) {
        UmsOrganizations umsOrganizations = new UmsOrganizations();
        umsOrganizations.setOrgName(updateDTO.getOrgName());
        umsOrganizations.setParentOrgId(updateDTO.getParentOrgId());
        umsOrganizations.setOrgType(updateDTO.getOrgType());
        QueryWrapper<UmsOrganizations> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id",updateDTO.getOrgId());
        int update=umsOrganizationsMapper.update(umsOrganizations, queryWrapper);
        if (update < 0) {
            throw new BusinessException(DataBaseEnum.UPDATE_FAILED);
        }
    }

    @Override
    public void addUserInOrganization(UmsOrganizationUserAddDTO addDTO) {
        List<String> userIds = addDTO.getUserIds();
        String organizationId = addDTO.getOrganizationId();
        for (String userId : userIds) {
            umsUserOrganizationService.updateUserOrganization(userId,organizationId);
        }
    }

    @Override
    public void deleteUserInOrganization(UmsOrganizationUserDeleteDTO deleteDTO) {
        List<String> userIds = deleteDTO.getUserIds();
        String organizationId = deleteDTO.getOrganizationId();
        for (String userId : userIds) {
            umsUserOrganizationService.deleteUserOrganization(userId,organizationId);
        }
    }

    @Override
    public UmsOrganizationFindAllDTO getAllOrganization(String rootId) {
        //查询所有组织架构的数据
        List<UmsOrganizationFindAllDTO> nodes = umsOrganizationsMapper.getAllOrganization(rootId);

        //构建树形结构
        Map<String,UmsOrganizationFindAllDTO> nodeMap = new HashMap<>();
        UmsOrganizationFindAllDTO rootNode=null;

        // 将所有节点存入 Map
        for (UmsOrganizationFindAllDTO node : nodes) {
            nodeMap.put(node.getOrgId(), node);
            if (node.getParentOrgId() == null) {
                rootNode = node;
            }
        }

        //构建树形结构
        for (UmsOrganizationFindAllDTO node : nodes) {
            if (node.getParentOrgId() != null) {
                UmsOrganizationFindAllDTO parentNode = nodeMap.get(node.getParentOrgId());
                if (parentNode != null) {
                    if (parentNode.getChildren() == null) {
                        parentNode.setChildren(new ArrayList<>());
                    }
                    parentNode.getChildren().add(node);
                }
            }
        }

        return rootNode;
    }
}
